package com.bl.student.dao.redis;

import com.bl.student.common.enums.ArenaType;
import com.bl.student.common.utils.DateUtils;
import lombok.extern.log4j.Log4j2;
import org.assertj.core.util.Strings;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by yujingyi on 2018/3/15.
 */
@Component
@Log4j2
public class ArenaDao extends AbstractDao {

    private final String ARENA_DURATION_KEY = "ARENA_DURATION_";
    private final String ARENA_CUR_START_TIME_KEY = "ARENA_CUR_START_TIME_";
    private final String ARENA_CUR_END_TIME_KEY = "ARENA_CUR_END_TIME_";
    private final String USER_START_ARENA_NUM_TODAY = "USER_START_ARENA_NUM_TODAY_";

    private final int MILLISECONDS_OF_DAY = 3600 * 1000 * 24;

    /**
     * 设置擂台持续豪秒时间(下期开始生效)
     * @param duration
     */
    public void setArenaDuration(long duration, ArenaType arenaType) {
        set(genKey(ARENA_DURATION_KEY, arenaType.toString()), duration + "");
    }

    public long getArenaDuration(ArenaType arenaType) {
        return Long.valueOf(get(genKey(ARENA_DURATION_KEY, arenaType.toString())));
    }

    /**
     * 设置当前擂台开始毫秒时间
     * @return
     */
    public void setCurArenaStartTime(long startTime, ArenaType arenaType) {
        set(genKey(ARENA_CUR_START_TIME_KEY, arenaType.toString()), startTime + "");
    }

    public long getCurArenaStartTime(ArenaType arenaType) {
        return Long.valueOf(get(genKey(ARENA_CUR_START_TIME_KEY, arenaType.toString())));
    }

    /**
     * 设置当前擂台结束毫秒时间
     * @param endTime
     */
    public void setCurArenaEndTime(long endTime, ArenaType arenaType) {
        set(genKey(ARENA_CUR_END_TIME_KEY, arenaType.toString()), endTime + "");
    }

    public long getCurArenaEndTime(ArenaType arenaType) {
        return Long.valueOf(get(genKey(ARENA_CUR_END_TIME_KEY, arenaType.toString())));
    }

    /**
     * 获取用户一天发贴次数
     * @param account
     * @return
     */
    public long getUserStartArenaNumToday(String account, ArenaType arenaType) {
        String today = DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYY_MM_DD);
        String num = get(genKey(USER_START_ARENA_NUM_TODAY, account, today, arenaType.toString()));
        return Strings.isNullOrEmpty(num) ? 0 : Long.valueOf(num);
    }

    /**
     * 增加用户发贴次数
     * @param account
     * @return
     */
    public long incrUserStartArenaNumToday(String account, ArenaType arenaType) {
        String today = DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYY_MM_DD);
        // TODO: 2018/3/19 modify milliseconds of day to left milliseconds of today
        return incr(genKey(USER_START_ARENA_NUM_TODAY, account, today, arenaType.toString()), MILLISECONDS_OF_DAY);
    }
}
